/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.MySQL;

import ClassesUnicas.NivelAcesso;
import DAO.DAONivelAcesso;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * DAONivelAcessoMySQL

 Versao 1.0

 Data Criação 25/03/2014
 *
 * @author Igor
 */
public class DAONivelAcessoMySQL extends DAOConexaoMySQL implements DAONivelAcesso {

    @Override
    public NivelAcesso localizaNivelAcesso(long idNivelAcesso) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public NivelAcesso localizaNivelAcesso(String descricao) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ArrayList<NivelAcesso> localizaNiveisAcesso(String idNivelAcesso, String descricao, String orderBy, String nivelAcessoDoProfissionalLogado) throws Exception {
        ResultSet result = null;
        ArrayList<NivelAcesso> tiposProfissional = new ArrayList<NivelAcesso>();
        String clausulaWhere = "";
        String clausulaOrderBy = "";

        //Cria clausula where
        if(nivelAcessoDoProfissionalLogado != null){
            if (clausulaWhere.isEmpty()) {
                clausulaWhere = "where ";
            } else {
                clausulaWhere = clausulaWhere + " and ";
            }
            clausulaWhere = clausulaWhere + " a.ID_NIVEL_ACESSO >= " + nivelAcessoDoProfissionalLogado;
        }
        
        if (idNivelAcesso != null) {
            if (clausulaWhere.isEmpty()) {
                clausulaWhere = "where ";
            } else {
                clausulaWhere = clausulaWhere + " and ";
            }
            clausulaWhere = clausulaWhere + " a.ID_NIVEL_ACESSO = " + idNivelAcesso;
        }

        if (descricao != null) {
            if (clausulaWhere.isEmpty()) {
                clausulaWhere = "where ";
            } else {
                clausulaWhere = clausulaWhere + " and ";
            }
            clausulaWhere = clausulaWhere + " a.DESCRICAO like '%" + descricao + "%'";
        }

        //OrderBy
        if (orderBy != null) {
            clausulaOrderBy = " order by " + orderBy;
        }

        result = this.select.executeQuery("select\n"
                + "a.ID_NIVEL_ACESSO,\n"
                + "a.DESCRICAO\n"
                + "from nivel_acesso a\n"
                + clausulaWhere
                + clausulaOrderBy);

        while (result.next()) {
            NivelAcesso nivelAcesso = new NivelAcesso();

            nivelAcesso.setIdNivelAcesso(result.getLong("ID_NIVEL_ACESSO"));
            nivelAcesso.setDescricao(result.getString("DESCRICAO"));
            tiposProfissional.add(nivelAcesso);

        }

        return tiposProfissional;
    }

}
